package com.eduService.utils;

import lombok.Data;

import java.util.HashMap;

@Data
public class R {
    //    是否成功
    private Boolean success;
    //    状态码
    private Integer code;
    //    返回信息
    private String msg;
    //    返回数据
    private HashMap<String, Object> data = new HashMap<String, Object>();

    //    私有构造方法，使外部无法创建实例，只能调用该类的静态方法
    private R() {
    }

    //    调用成功返回
    public static R ok() {
        R r = new R();
        r.setSuccess(true);
        r.setCode(IdataFormat.SUCCESS);
        r.setMsg("请求成功！");
        return r;
    }

    //    调用失败返回
    public static R error() {
        R r = new R();
        r.setSuccess(false);
        r.setCode(IdataFormat.FAIL);
        r.setMsg("请求失败！");
        return r;
    }

    public R success(Boolean success) {
        this.setSuccess(success);
        return this;
    }

    public R msg(String msg) {
        this.setMsg(msg);
        return this;
    }

    public R code(Integer code) {
        this.setCode(code);
        return this;
    }

    public R data(String key, Object value) {
        this.data.put(key, value);
        return this;
    }

    public R data(HashMap<String, Object> map) {
        this.setData(map);
        return this;
    }
}
